package com.example.lenovo.handlerthread;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;


public class MainActivity extends AppCompatActivity {
    private Button button=null;
    private Button button2=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button =(Button)findViewById(R.id.button);
        button.setOnClickListener(new StartButtonListerner());
        button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new EndButtonListerne());


    }
    Handler handler=new Handler();
    class StartButtonListerner implements OnClickListener{

        public void onClick(View v){
            handler.post(updateThread);
        }
    }
    class EndButtonListerne implements OnClickListener{
        public void onClick(View v){
            handler.removeCallbacks(updateThread);
        }
    }
    Runnable updateThread=new Runnable() {
        @Override
        public void run() {
            System.out.print("Update Thread");
            Log.v("tag", "Update Thread");
            handler.postDelayed(updateThread, 2000);
        }

    };
}
